package com.my.system.controller;

import com.my.constant.UserConstants;
import com.my.core.controller.BaseController;
import com.my.core.page.TableDataInfo;
import com.my.response.Result;
import com.my.system.entity.SysUser;
import com.my.system.service.ISysUserService;
import com.my.system.service.impl.SysUserService;
import com.my.utils.StringUtils;
import com.my.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author my
 * @since 2021-04-21
 */
@RestController
@RequestMapping("/user")
@Api(value = "系统管理模块", tags = "系统用户接口")
public class SysUserController extends BaseController {

    private ISysUserService userService;

    public SysUserController(SysUserService userService) {
        this.userService = userService;
    }

    @ApiOperation("分页查询用户列表")
    @GetMapping("/list")
    public TableDataInfo list(SysUser user) {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    @ApiOperation("分页查询用户信息列表")
    @PostMapping("/findUserFactor")
    public Result findUserFactor(@RequestBody UserVO userVO) {
        return userService.findUserFactor(userVO);
    }

    @ApiOperation(value = "用户列表", notes = "根据用户编号获取详细信息,新增时执行")
    @GetMapping(value = {"/", "/{userId}"})
    public Result getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        return userService.getInfo(userId);
    }

    @ApiOperation("新增用户")
    @PostMapping("/setUser")
    public Result setUser(@RequestBody SysUser user) {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
            return Result.error("").message("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return Result.error("").message("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail())
                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return Result.error("").message("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        //user.setCreateBy(SecurityUtils.getUsername());
        // 密码加密
        // user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.insertUser(user));
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delUser/{userIds}")
    public Result delUser(@PathVariable Long[] userIds) {
        return toAjax(userService.deleteUserByIds(userIds));
    }

    @ApiOperation("修改用户")
    @PutMapping("/setUser")
    public Result editUser(@RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return Result.error("").message("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail())
                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return Result.error("").message("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        // user.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(userService.updateUser(user));
    }

    @ApiOperation("用户导入模板")
    @GetMapping("/importTemplate")
    public Result importTemplate() {
        // ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return null;// util.importTemplateExcel("用户数据");
    }

    @ApiOperation("用户导出")
    @GetMapping("/export")
    public Result export(SysUser user) {
        List<SysUser> list = userService.selectUserList(user);
        // ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return null;// util.exportExcel(list, "用户数据");
    }

    @ApiOperation("状态用户修改")
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        return toAjax(userService.updateUserStatus(user));
    }

}

